<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      :data="teaData"
      
    >
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name" style="width:200px"></el-input>
      </el-form-item>
      </el-form-item>
      <el-form-item label="登录密码" prop="password">
        <el-input v-model="ruleForm.password" style="width:200px"></el-input>
      </el-form-item>
      <el-form-item label="任教班级" prop="clazz">
        <el-input v-model="ruleForm.clazz" style="width:200px"></el-input>
      </el-form-item>
      <el-form-item label="任职教室" prop="room">
        <el-input v-model="ruleForm.room" style="width:200px"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleAdd">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      teaData: [],
      ruleForm: {
        clazz: null,
        name: "",
        password: "",
        room: null,
      },
      rules: {
        name: [
          { required: true, message: "请完善信息", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        clazz: [{ required: true, message: "请完善班级信息", trigger: "blur" }],
        password: [{ required: true, message: "请完善密码", trigger: "blur" }],
        room: [
          { required: true, message: "请完善任职教室信息", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    mounted() {
      this.initTea();
    },
    initTea() {
      this.postRequest(url).then((res) => {
        this.loading = true;
        if (res) {
          this.loading = false;
          this.teaData = res.data;
          this.total = res.total;
        }
      });
    },
    handleAdd() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          
            this.postRequest("/teacher/add", this.ruleForm).then((res) => {
              if (res) {
                this.dialogVisible = false;
                this.initTea();
              }
            });
          
        } else {
          this.$message.warning("请完善表单信息！");
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
.el-form-item {
  text-align: right;
}
</style>
